home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Snippets / Solver / Help.c next >
Encoding:
Text File  |  1995-08-27  |  5.2 KB  |  216 lines  |  [TEXT/SPM ]

  1. //• Helper.c - from Solver 1.1 ©1995 by Stefan C. Sinclair. All Rights Reserved.
  2.  
  3. #include <limits.h>
  4.  
  5. #define kBaseResID            128
  6. #define kMoveToFront        (WindowPtr)-1L
  7. #define kScrollBarWidth        16
  8. #define    kNilActionProc        nil
  9. #define kSleep                LONG_MAX
  10.  
  11. #define    kVisible            true
  12. #define    kStartValue            1
  13. #define    kMinValue            1
  14. #define    kNilRefCon            0L
  15. #define    kEmptyTitle            "\p"
  16.  
  17. #define    kEmptyString        "\p"
  18. #define kNilFilterProc        nil
  19.  
  20. #define    kErrorAlertID        kBaseResID
  21.  
  22.  
  23. /**************/
  24. /*  Globals      */
  25. /**************/
  26.  
  27. Boolean                ggDone;
  28. ControlActionUPP    gActionUPP; //• This is for PowerPC!
  29. extern Rect        helpRect;
  30. extern PicHandle    helpPicture;
  31. extern WindowPtr    hwindow;
  32.  
  33.  
  34. /*  Function Prototypes  */
  35. void    Helper( void );
  36. void    HelpWindowInit( void );
  37. void    SetUpScrollBar( WindowPtr window );
  38. pascal void ScrollProc( ControlHandle theControl, short partCode );
  39. void    HelpEventLoop( void );
  40. void    HelpDoEvent( EventRecord *eventPtr );
  41. void    HelpHandleMouseDown( EventRecord *eventPtr );
  42. void    HelpUpdateWindow( WindowPtr window );
  43.  
  44. //• The main online help routine; resembles a mini-application.
  45. void    Helper(void)
  46. {
  47.     HelpWindowInit();
  48.     HelpEventLoop();
  49.     HideWindow(hwindow);
  50. }
  51.  
  52. //• HelpWindowInit() - actually, the window has already been initialized. Here, we just bring it to the front.
  53. void    HelpWindowInit(void)
  54. {
  55.     ShowWindow( hwindow );
  56.     SetPort( hwindow );
  57. }
  58.  
  59.  
  60. /**********************************    SetUpScrollBar    *******/
  61. void    SetUpScrollBar( WindowPtr window )
  62. {
  63.     Rect            vScrollRect;
  64.     short            numViews = 4; //• I have chosen to divide the big PICT into 4 sections here.
  65.     ControlHandle    scrollBarH;
  66.     
  67.     vScrollRect = window->portRect;
  68.     vScrollRect.top -= 1;
  69.     vScrollRect.bottom += 1;
  70.     vScrollRect.left = vScrollRect.right - kScrollBarWidth + 1;
  71.     vScrollRect.right += 1;
  72.     
  73.     scrollBarH = NewControl( window, &vScrollRect,
  74.             kEmptyTitle, kVisible, kStartValue, kMinValue,
  75.             numViews, scrollBarProc, kNilRefCon );
  76. }
  77.  
  78. //• ScrollProc(); when scrolling, the entire picture is still being drawn, but only 1/4
  79. //• of it will be visible through the help window. This is achieved by offsetting the help
  80. //• PICT's rect by 1/4 of it's height each time the scroll bar is hit.
  81. pascal void ScrollProc( ControlHandle theControl, short partCode )
  82. {
  83.     short        curCtlValue, maxCtlValue, minCtlValue;
  84.     WindowPtr    window;
  85.     
  86.     maxCtlValue = GetCtlMax( theControl );
  87.     curCtlValue = GetCtlValue( theControl );
  88.     minCtlValue = GetCtlMin( theControl );
  89.     
  90.     window = (**theControl).contrlOwner;
  91.     
  92.     switch ( partCode )
  93.     {
  94.         case inPageDown:
  95.         case inDownButton:
  96.             if ( curCtlValue < maxCtlValue )
  97.             {
  98.                 curCtlValue += 1;
  99.                 SetCtlValue( theControl, curCtlValue );
  100.                 helpRect.top-=205; // 1/4 of my PICT's height
  101.                 helpRect.bottom-=205;
  102.                 HelpUpdateWindow( window );
  103.             }
  104.             break;
  105.         case inPageUp:
  106.         case inUpButton:
  107.             if ( curCtlValue > minCtlValue )
  108.             {
  109.                 curCtlValue -= 1;
  110.                 SetCtlValue( theControl, curCtlValue );
  111.                 helpRect.top+=205; // 1/4 of my PICT's height
  112.                 helpRect.bottom+=205;
  113.                 HelpUpdateWindow( window );
  114.             }
  115.     }
  116. }
  117.  
  118. //• HelpEventLoop(); an event loop inside of an event loop.
  119. void    HelpEventLoop(void)
  120. {        
  121.     EventRecord        event;
  122.     
  123.     ggDone = false;
  124.     
  125.     while ( ggDone == false )
  126.     {
  127.         if ( WaitNextEvent( everyEvent, &event, kSleep, nil ) )
  128.             HelpDoEvent( &event );
  129.     }
  130. }
  131.  
  132. //• HelpDoEvent() - user's actions are pretty limited while in the online help window, as you will see.
  133. void    HelpDoEvent( EventRecord *eventPtr )
  134. {
  135.     WindowPtr    window;
  136.     
  137.     switch ( eventPtr->what )
  138.     {
  139.         case mouseDown: 
  140.             HelpHandleMouseDown( eventPtr );
  141.             break;
  142.         case updateEvt:
  143.             window = (WindowPtr)eventPtr->message;
  144.             
  145.             BeginUpdate( window );
  146.             DrawControls( window );
  147.             HelpUpdateWindow( window );
  148.             EndUpdate( window );
  149.             break;
  150.     }
  151. }
  152.  
  153. //• HelpHandleMouseDown() - user's actions are pretty limited while in the online help window, as you will see.
  154. void    HelpHandleMouseDown(EventRecord *eventPtr)
  155. {
  156.     WindowPtr            window;
  157.     short                thePart;
  158.     Point                thePoint;
  159.     ControlHandle        theControl;
  160.     
  161.     thePart = FindWindow( eventPtr->where, &window );
  162.     switch ( thePart )
  163.     {
  164.         case inSysWindow : 
  165.             SystemClick( eventPtr, window );
  166.             break;
  167.         case inDrag : 
  168.             DragWindow( window, eventPtr->where, &qd.screenBits.bounds );
  169.             break;
  170.         case inContent:
  171.             thePoint = eventPtr->where;
  172.             GlobalToLocal( &thePoint );
  173.             
  174.             thePart = FindControl( thePoint, window, &theControl );
  175.             //• Check for scroll bar event
  176.             if ( theControl == ((WindowPeek)window)->controlList )
  177.             {
  178.                 if ( thePart == inThumb )
  179.                 {
  180.                     thePart = TrackControl( theControl, thePoint, kNilActionProc );
  181.                     InvalRect( &(window->portRect) );
  182.                 }
  183.                 else
  184.                 {
  185.                     gActionUPP = NewControlActionProc( ScrollProc ); //• PPC again!
  186.                     thePart = TrackControl( theControl, thePoint, gActionUPP );
  187.                 }
  188.             }
  189.             break;
  190.         case inGoAway: //• Leave help now?
  191.             if(TrackGoAway(window, eventPtr->where)) 
  192.             ggDone = true;
  193.             break;
  194.     }
  195. }
  196.  
  197. //• HelpUpdateWindow() - basically just redraws the tall hel PICT
  198. void    HelpUpdateWindow(WindowPtr window)
  199. {
  200.     Rect        windowRect;
  201.     RgnHandle    tempRgn;
  202.     
  203.     tempRgn = NewRgn();
  204.     GetClip( tempRgn );
  205.     
  206.     windowRect = window->portRect;
  207.     windowRect.right -= kScrollBarWidth;
  208.     EraseRect( &windowRect );
  209.     
  210.     ClipRect( &windowRect );
  211.     
  212.     DrawPicture( helpPicture, &helpRect );
  213.     
  214.     SetClip( tempRgn );
  215.     DisposeRgn( tempRgn );
  216. }